package com.huawei.hms.framework.network.restclient.hwhttp;

import com.huawei.hms.framework.common.Logger;
import com.huawei.hms.framework.common.NetworkUtil;
import com.huawei.hms.framework.common.Utils;
import com.huawei.hms.framework.network.restclient.hianalytics.DefaultRCEventListener;
import com.huawei.hms.framework.network.restclient.hianalytics.EditableMetrics;
import com.huawei.hms.framework.network.restclient.hianalytics.RCEventListener;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo;
import com.huawei.hms.framework.network.restclient.hianalytics.RequestFinishedInfo.Metrics;
import com.huawei.hms.framework.network.restclient.hwhttp.Interceptor.Chain;
import com.huawei.hms.framework.network.restclient.hwhttp.RequestTask.Factory;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DNManager;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsUtil;
import com.huawei.hms.framework.network.restclient.hwhttp.netdiag.NetDiagManager;
import com.huawei.hms.framework.network.util.ContextUtil;
import java.io.IOException;

public class RetryInterceptor implements Interceptor {
    private static final String TAG = "RetryInterceptor";
    private volatile boolean canceled;
    private Factory factory;
    private RequestTask requestTask;
    private int retryTime = 0;

    public Response intercept(Chain chain) throws IOException {
        RealInterceptorChain realInterceptorChain = (RealInterceptorChain) chain;
        RCEventListener rCEventListener = realInterceptorChain.getRCEventListener();
        HttpClient client = realInterceptorChain.getClient();
        Request request = chain.request();
        String host = request.getUrl().getHost();
        while (!isCanceled()) {
            this.requestTask = client.getFactory(request).newTask();
            long currentTime = Utils.getCurrentTime(true);
            try {
                rCEventListener.retryInterceptorStart(request, this.requestTask);
                Response proceed = realInterceptorChain.proceed(request, this.requestTask);
                rCEventListener.retryInterceptorEnd(proceed);
                NetDiagManager.resetCount();
                setDnsType(host);
                DnsUtil.doRespone(currentTime, host, Integer.valueOf(proceed.getCode()));
                return proceed;
            } catch (Throwable e) {
                if (this.retryTime == 0) {
                    netDiag(rCEventListener);
                }
                setDnsType(host);
                DnsUtil.doRespone(currentTime, host, e);
                rCEventListener.retryInterceptorFailed(e);
                if (isRetry(request, client)) {
                    Logger.w(TAG, "intercept IOException, retry " + this.retryTime, e);
                    this.retryTime++;
                } else {
                    Logger.w(TAG, "intercept IOException end");
                    throw e;
                }
            }
        }
        throw new IOException("Canceled");
    }

    public void cancel() {
        this.canceled = true;
        if (this.requestTask != null) {
            this.requestTask.cancel();
        }
    }

    public boolean isCanceled() {
        return this.canceled || (this.requestTask != null && this.requestTask.isCanceled());
    }

    private boolean isRetry(Request request, HttpClient httpClient) {
        if (request.getRetryTimeOnConnectionFailure() <= 0 || this.retryTime >= request.getRetryTimeOnConnectionFailure()) {
            return false;
        }
        if (httpClient.isWeakNetworkRetryEnable() || NetworkUtil.getNetworkType(ContextUtil.getContext()) != -1) {
            return true;
        }
        return false;
    }

    protected RequestTask getRequestTask() {
        return this.requestTask;
    }

    private void setDnsType(String str) {
        RequestFinishedInfo requestFinishedInfo = getRequestTask().getRequestFinishedInfo();
        if (requestFinishedInfo != null) {
            Metrics metrics = requestFinishedInfo.getMetrics();
            if (metrics instanceof EditableMetrics) {
                ((EditableMetrics) metrics).setDnsType(DNManager.getInstance().getResolverAlph(str));
            }
        }
    }

    private void netDiag(final RCEventListener rCEventListener) {
        final NetDiagManager netDiagManager = new NetDiagManager();
        netDiagManager.getNetDiagExecutor().execute(new Runnable() {
            public void run() {
                if (rCEventListener instanceof DefaultRCEventListener) {
                    Logger.v(RetryInterceptor.TAG, "netDiag start");
                    ((DefaultRCEventListener) rCEventListener).setNetDiagManager(netDiagManager);
                    netDiagManager.netDiag();
                }
            }
        });
    }
}
